from datetime import datetime
from typing import List, Dict, Any, Optional
from pydantic import BaseModel

# 新增的创建和更新模型
class ImageCreate(BaseModel):
    """图片创建模型"""
    entity_id: str
    group_id: str
    pic_name: str
    original_filename: Optional[str] = None
    cos_url: Optional[str] = None
    file_size: Optional[int] = None
    file_type: Optional[str] = None
    width: Optional[int] = None
    height: Optional[int] = None
    tencent_tags: Optional[str] = None
    custom_content: Optional[str] = None
    upload_status: Optional[str] = "success"
    tencent_response: Optional[str] = None
    user_id: Optional[int] = None

class ImageUpdate(BaseModel):
    """图片更新模型"""
    pic_name: Optional[str] = None
    original_filename: Optional[str] = None
    cos_url: Optional[str] = None
    file_size: Optional[int] = None
    file_type: Optional[str] = None
    width: Optional[int] = None
    height: Optional[int] = None
    tencent_tags: Optional[str] = None
    custom_content: Optional[str] = None
    upload_status: Optional[str] = None
    tencent_response: Optional[str] = None

class ImageUploadResponse(BaseModel):
    """图片上传响应"""
    success: bool
    message: str
    entity_id: Optional[str] = None
    pic_name: Optional[str] = None
    url: Optional[str] = None  # 新增URL字段
    data: Optional[Dict[str, Any]] = None

# 新增图片详情响应
class ImageDetailResponse(BaseModel):
    """图片详情响应"""
    id: int
    entity_id: str
    group_id: str
    pic_name: str
    original_filename: Optional[str]
    cos_url: Optional[str]
    file_size: Optional[int]
    file_type: Optional[str]
    width: Optional[int]
    height: Optional[int]
    created_at: datetime
    
    class Config:
        from_attributes = True
        # 配置时间序列化格式为易读格式
        json_encoders = {
            datetime: lambda v: v.strftime("%Y-%m-%d %H:%M:%S") if v else None
        }

# 相关标签模型（复用product.py中的定义）
class RelatedTag(BaseModel):
    """相关标签模型"""
    tag: str
    count: int
    weight: float

# 新增图片搜索项模型
class ImageSearchItem(BaseModel):
    """图片搜索结果项"""
    id: int
    entity_id: Optional[str]
    pic_name: str  # 对应 ProductTitle
    cos_url: Optional[str]  # 对应 CosUrl
    width: Optional[int]
    height: Optional[int]
    file_size: Optional[int] = None
    created_at: datetime
    score: Optional[float] = None  # 搜索相似度得分
    product_tags: Optional[List[str]] = None  # 修改：产品标签改为字符串数组
    
    class Config:
        from_attributes = True
        json_encoders = {
            datetime: lambda v: v.strftime("%Y-%m-%d %H:%M:%S") if v else None
        }

class ImageSearchResponse(BaseModel):
    """图片搜索响应"""
    success: bool
    data: List[ImageSearchItem]  # 改为 data
    count: int
    related_tags: List[RelatedTag]  # 新增：相关标签列表

class ImageInfo(BaseModel):
    """图片信息"""
    entity_id: str
    score: float
    tags: Optional[Dict[str, str]] = None

# 新增的响应模型
class GroupInfo(BaseModel):
    """图片库信息"""
    group_id: str
    group_name: str
    brief: Optional[str] = None
    create_time: str
    update_time: str
    pic_count: int
    max_capacity: int
    max_qps: int
    group_type: int

class GroupListResponse(BaseModel):
    """图片库列表响应"""
    success: bool
    groups: List[GroupInfo]
    count: int

class ImageDetailInfo(BaseModel):
    """详细图片信息"""
    entity_id: str
    pic_name: str
    custom_content: Optional[str] = None
    tags: Optional[str] = None
    score: float

class ImageListResponse(BaseModel):
    """图片列表响应"""
    success: bool
    group_id: str
    entity_id: str
    images: List[ImageDetailInfo]
    count: int

# 在文件末尾添加瀑布流相关的响应模型
class WaterfallImageItem(BaseModel):
    """瀑布流图片项"""
    id: int
    entity_id: str
    pic_name: str
    cos_url: Optional[str]
    width: Optional[int]
    height: Optional[int]
    file_size: Optional[int]
    created_at: datetime
    
    class Config:
        from_attributes = True
        # 配置时间序列化格式为易读格式
        json_encoders = {
            datetime: lambda v: v.strftime("%Y-%m-%d %H:%M:%S") if v else None
        }

class WaterfallResponse(BaseModel):
    """瀑布流响应"""
    success: bool
    data: List[WaterfallImageItem]
    total: int
    page: int
    page_size: int
    has_more: bool
